package com.liwang.lanouhn.nvshengo.activities;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.liwang.lanouhn.nvshengo.Info.Info;
import com.liwang.lanouhn.nvshengo.R;
import com.liwang.lanouhn.nvshengo.fragments.FeatureFragment;

import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;

/**
 * Created by lanouhn on 2016/10/11.
 */
public class FeatureDetailsActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView iv_feature_back;
    private TextView tv_feature_title;
    private ImageView iv_feature_share;
    private ImageView iv_feature_like;
    private TextView tv_feature_like;
    private ImageView iv_favourite_feature,iv_line;
    private TextView tv_favourite_feature;
    private LinearLayout ll_favourite_feature;
    private LinearLayout ll_feature,ll_like;
    private FrameLayout feature;
    private FeatureFragment featureFragment;
    private android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
    private android.support.v4.app.FragmentTransaction transaction;
    Info featureInfo;
    private int times = 0;
    private int tag =0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        int flag= WindowManager.LayoutParams.FLAG_FULLSCREEN;
        Window window=getWindow();
        window.setFlags(flag,flag);
        setContentView(R.layout.activity_featuredetails);
        ShareSDK.initSDK(this);
        initView();
        initData();
        initEvent();
    }



    private void initView() {
        iv_line= (ImageView) findViewById(R.id.iv_line);
        iv_feature_back = (ImageView) findViewById(R.id.iv_feature_back);
        iv_feature_back.setOnClickListener(this);
        tv_feature_title = (TextView) findViewById(R.id.tv_feature_title);
        iv_feature_share = (ImageView) findViewById(R.id.iv_feature_share);
        iv_feature_share.setOnClickListener(this);
        iv_feature_like = (ImageView) findViewById(R.id.iv_feature_like);
        ll_like= (LinearLayout) findViewById(R.id.ll_like);
        ll_like.setOnClickListener(this);
        tv_feature_like = (TextView) findViewById(R.id.tv_feature_like);
        iv_favourite_feature = (ImageView) findViewById(R.id.iv_favourite_feature);
        tv_favourite_feature = (TextView) findViewById(R.id.tv_favourite_feature);
        ll_favourite_feature = (LinearLayout) findViewById(R.id.ll_favourite_feature);
        ll_favourite_feature.setOnClickListener(this);
        ll_feature = (LinearLayout) findViewById(R.id.ll_feature);
        feature= (FrameLayout) findViewById(R.id.feature);
        featureInfo = (Info) getIntent().getSerializableExtra("featureInfo");
        tv_feature_title.setText(featureInfo.getTitle());
        tv_feature_like.setText(featureInfo.getLike_count()+"");

    }
    private void initData() {
        featureFragment=FeatureFragment.newInstants(featureInfo);
        transaction=fm.beginTransaction();
        transaction.add(R.id.feature,featureFragment);
        transaction.commit();
    }
    private void initEvent() {

    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_feature_back:
                finish();
            break;
            case R.id.iv_feature_share:
                showShare();
                break;
            case R.id.ll_like:
                if (times == 0) {
                    iv_feature_like.setBackgroundResource(R.mipmap.ic_liked);
                    tv_feature_like.setText((featureInfo.getLike_count()+1)+"");
                    times = 1;
                } else {
                    iv_feature_like.setBackgroundResource(R.mipmap.ic_new_like);
                    tv_feature_like.setText(featureInfo.getLike_count()+"");
                    times = 0;
                }
                break;
            case R.id.ll_favourite_feature:
                if (tag ==0){
                    iv_line.setVisibility(View.VISIBLE);
                    ll_favourite_feature.setBackgroundColor(Color.WHITE);
                    iv_favourite_feature.setBackgroundResource(R.mipmap.btn_good_item_favourite_pressed);
                    tv_favourite_feature.setTextColor(getResources().getColor(R.color.gray));
                    tv_favourite_feature.setText("已收藏");
                    Toast.makeText(FeatureDetailsActivity.this,"收藏成功",Toast.LENGTH_SHORT).show();
                    tag=1;
                }else {
                    iv_line.setVisibility(View.GONE);
                    ll_favourite_feature.setBackgroundColor(getResources().getColor(R.color.pink));
                    iv_favourite_feature.setBackgroundResource(R.mipmap.btn_good_item_favourite);
                    tv_favourite_feature.setTextColor(Color.WHITE);
                    tv_favourite_feature.setText("收藏");
                    Toast.makeText(FeatureDetailsActivity.this,"取消收藏",Toast.LENGTH_SHORT).show();
                    tag=0;
                }
                break;


        }
    }
    private void showShare() {
        ShareSDK.initSDK(this);
        OnekeyShare oks = new OnekeyShare();
//关闭sso授权
        oks.disableSSOWhenAuthorize();

// title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间等使用
        oks.setTitle("女神 go!");
// titleUrl是标题的网络链接，QQ和QQ空间等使用
        oks.setTitleUrl(featureInfo.getUrl());
// text是分享文本，所有平台都需要这个字段
        oks.setText(featureInfo.getTitle());
// imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
//oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
// url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl(featureInfo.getUrl());
// comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("你的私人定制美丽管家");
// site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(getString(R.string.app_name));
// siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl(featureInfo.getUrl());

// 启动分享GUI
        oks.show(this);
    }
}
